# shift.py
#
# A shift cipher, also known as a rotation cipher or Caesar cipher, "rotates" 
# or increments each character in the message by the amount of the key.  
#
# Example: Plain text "hello" with a key of 2 increments each character by 2 to get "jgnnq"


###############################################################
# Encrypt a plain text message using a shift cipher
def shift_encrypt(plainText, key):

    # Friendly checks on the message and key
    if not len(plainText) > 0:
        print "Empty message"
        return False
    if not isinstance(key,int):
        print "Key must be an integer"
        return False

    # We will return only upper case alpha characters, ignoring spaces.
    alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    
    cipherText = ""
    
    # For each character in plainText
    for c in range(len(plainText)):
        # Increment alpha characters by key
        if plainText[c].isalpha():
            cipherText += alphabet[(alphabet.find(plainText[c].upper())+key)%26]
        else:
            cipherText += plainText[c]
            
    return cipherText
            

###############################################################
# Decrypt a plain text message using a shift cipher
def shift_decrypt(cipherText, key):

    # Friendly checks on the message and key
    if not len(cipherText) > 0:
        print "Empty message"
        return False
    if not isinstance(key,int):
        print "Key must be an integer"
        return False

    # We will return only upper case alpha characters, ignoring spaces.
    alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    
    plainText = ""
    
    # For each character in plainText
    for c in range(len(cipherText)):
        # Decrement alpha characters by key
        if cipherText[c].isalpha():
            plainText += alphabet[(alphabet.find(cipherText[c].upper())-key)%26]
        else:
            plainText += cipherText[c]
            
    return plainText
        
